package com.delivery.www.service.ServiceImpl;

import com.delivery.www.dao.CouponDao;
import com.delivery.www.po.Coupon;
import com.delivery.www.po.HaveCoupon;
import com.delivery.www.service.CouponService;
import com.delivery.www.constant.Constants;
import com.delivery.www.dao.DaoImpl.CouponDaoImpl;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 12970
 */
public class CouponServiceImpl implements CouponService {
    private final CouponDao couponDao = new CouponDaoImpl();

    @Override
    public List<Coupon> getCoupon(int storeId) {
        if (storeId <= 0) {
            return null;
        } else {
            return couponDao.getCoupon(storeId);
        }

    }

    @Override
    public boolean addCoupon(int storeId, Float value, Float min, int validity) {
        if (storeId <= 0 || value <= 0 || min < 0 || validity <= 0) {
            return false;
        } else {
            return couponDao.addCoupon(storeId, value, min, validity) > 0;
        }
    }

    @Override
    public boolean deleteCoupon(int couponId) {
        if (couponId <= 0) {
            return false;
        } else {
            return couponDao.deleteCoupon(couponId) > 0;
        }
    }

    @Override
    public boolean modifyCouponValue(int couponId, Float value) {
        if (couponId <= 0 || value < 0) {
            return false;
        } else {
            return couponDao.modifyCouponValue(couponId, value) > 0;
        }
    }

    @Override
    public boolean modifyCouponMin(int couponId, Float value) {
        if (couponId <= 0 || value < 0) {
            return false;
        }
        return couponDao.modifyCouponMin(couponId, value) > 0;
    }

    @Override
    public boolean modifyCouponValidity(int couponId, int value) {
        if (couponId <= 0 || value < 0) {
            return false;
        }
        return couponDao.modifyCouponValidity(couponId, value) > 0;
    }

    @Override
    public List<Coupon> getUserCoupon(List<HaveCoupon> list, Float money) {
        List<Coupon> coupons1 = null;
        List<Coupon> coupons2 = new ArrayList<>();
        if (list.size() != 0) {
            coupons1 = couponDao.getUserCoupon(list);
        }
        if (coupons1 != null) {
            for (Coupon coupon : coupons1) {
                if (coupon.getMin() <= money) {
                    coupons2.add(coupon);
                }
            }
        }
        return coupons2;
    }

    @Override
    public List<Coupon> getUserCoupon(List<HaveCoupon> list) {

        if (list != null) {
            return couponDao.getUserCoupon(list);
        } else {
            return null;
        }
    }

    @Override
    public List<Coupon> getAllCoupon() {
        return couponDao.getAllCoupon();
    }

    @Override
    public boolean judgeField(int couponId, String field, String value) {
        if (couponId<=0||field==null||value==null){
            return false;
        }else {
            boolean b = false;
            if (field.equals(Constants.VALUE)) {
                Float valueOf = Float.valueOf(value);
                b = modifyCouponValue(couponId, valueOf);
            }
            if (field.equals(Constants.MIN)) {
                Float valueOf = Float.valueOf(value);
                b = modifyCouponMin(couponId, valueOf);
            }
            if (field.equals(Constants.VALIDITY)) {
                int valueOf = Integer.parseInt(value);
                b = modifyCouponValidity(couponId, valueOf);
            }
            return b;
        }
    }

}
